package com.codeckz.library_face.utils

import android.R
import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.graphics.Bitmap
import android.graphics.Point
import android.graphics.Rect
import android.os.Build
import android.view.Display
import android.view.View
import android.view.WindowManager





/**
 *@packageName com.codeckz.library_face.utils
 *@author kzcai
 *@date 2022/1/7
 */
class ScreenUtils private constructor() {
    companion object {
        /**
         * 获得屏幕宽度
         *
         * @param context
         * @return
         */
        fun getScreenWidth(context: Context): Int {
            val outSize = Point()
            val manager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager

            val width = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                manager.currentWindowMetrics.bounds.right
            } else {
                manager.defaultDisplay.getRealSize(outSize)
                outSize.x
            }
            return width
        }

        /**
         * 获得屏幕高度
         *
         * @param context
         * @return
         */
        fun getScreenHeight(context: Context): Int {
            val outSize = Point()
            val manager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val height = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                manager.currentWindowMetrics.bounds.bottom
            } else {
              manager.defaultDisplay.getRealSize(outSize)
                outSize.y
            }
            return height
        }



        /**
         * 获得状态栏的高度
         *
         * @param context
         * @return
         */
        fun getStatusHeight(context: Context): Int {
            var statusHeight = -1
            try {
                val clazz = Class.forName("com.android.internal.R\$dimen.xml")
                val `object` = clazz.newInstance()
                val height = clazz.getField("status_bar_height")[`object`].toString().toInt()
                statusHeight = context.getResources().getDimensionPixelSize(height)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return statusHeight
        }

        /**
         * 获取当前屏幕截图，包含状态栏
         *
         * @param activity
         * @return
         */
        fun snapShotWithStatusBar(activity: Activity): Bitmap? {
            val view: View = activity.window.decorView
            view.setDrawingCacheEnabled(true)
            view.buildDrawingCache()
            val bmp: Bitmap = view.getDrawingCache()
            val width = getScreenWidth(activity)
            val height = getScreenHeight(activity)
            var bp: Bitmap? = null
            bp = Bitmap.createBitmap(bmp, 0, 0, width, height)
            view.destroyDrawingCache()
            return bp
        }

        /**
         * 获取当前屏幕截图，不包含状态栏
         *
         * @param activity
         * @return
         */
        fun snapShotWithoutStatusBar(activity: Activity): Bitmap? {
            val view: View = activity.window.decorView
            view.setDrawingCacheEnabled(true)
            view.buildDrawingCache()
            val bmp: Bitmap = view.getDrawingCache()
            val frame = Rect()
            activity.window.decorView.getWindowVisibleDisplayFrame(frame)
            val statusBarHeight: Int = frame.top
            val width = getScreenWidth(activity)
            val height = getScreenHeight(activity)
            var bp: Bitmap? = null
            bp = Bitmap.createBitmap(
                bmp, 0, statusBarHeight, width, height
                        - statusBarHeight
            )
            view.destroyDrawingCache()
            return bp
        }

        fun getNavigationBarHeight(context: Context): Int {

            val height: Int
            val manager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager

            val display: Display = manager.defaultDisplay
            val point = Point()
            display.getRealSize(point)
            val decorView = (context as Activity).window.decorView
            val conf: Configuration = context.getResources().configuration
            height = if (Configuration.ORIENTATION_LANDSCAPE === conf.orientation) {
                val contentView = decorView.findViewById<View>(R.id.content)
                Math.abs(point.x - contentView.width)
            } else {
                val rect = Rect()
                decorView.getWindowVisibleDisplayFrame(rect)
                Math.abs(rect.bottom - point.y)
            }
            return height
        }
    }




    init {
        /* cannot be instantiated */
        throw UnsupportedOperationException("cannot be instantiated")
    }
}
